using System;
using Microsoft.SharePoint;
using System.Runtime.Remoting;
using System.Security.Policy;
using System.Reflection;
using System.Security.Principal;

namespace PhilWicklund.com.PollingWebPart.Utilities
{
    public class RemoteInterfaceFactory : IDisposable
    {
        private AppDomain appDomain = null;
        private IWSSUtilities remoteInterface = null;

        public IWSSUtilities Loader
        {
            get
            {
                return remoteInterface;
            }
        }  // End of property Loader

        /// <summary>
        /// Creates an App Domain using "c:\inetpub\wwwroot\" as the application base
        /// </summary>
        public RemoteInterfaceFactory()
        {
            createAppDomain(@"c:\inetpub\wwwroot\");
        }

        /// <summary>
        /// Creates an App Domain at the specified application base
        /// </summary>
        /// <param name="applicationBase">
        /// location of the root of the file structure for the application,
        /// in most cases, this is the value of this.Page.MapPath("/")
        /// </param>
        //		public RemoteInterfaceFactory (string applicationBase) 
        //		{
        //			createAppDomain(applicationBase);
        //		}

        private void createAppDomain(string applicationBase)
        {
            //			AppDomainSetup appDomainSetup       = new AppDomainSetup(); 
            //			appDomainSetup.ApplicationBase      = applicationBase;  // this is the value of this.Page.MapPath("/")
            //			appDomainSetup.ApplicationName      = AppDomain.CurrentDomain.SetupInformation.ApplicationName + "_Utilities";
            //			appDomainSetup.CachePath            = AppDomain.CurrentDomain.SetupInformation.CachePath;
            //			appDomainSetup.ConfigurationFile    = "web.config"; 
            //			appDomainSetup.DisallowCodeDownload = true;
            //			appDomainSetup.PrivateBinPath       = "bin";
            //			appDomainSetup.PrivateBinPathProbe  = "true";
            //			appDomainSetup.ShadowCopyFiles      = "true";
            //      
            //			this.appDomain = AppDomain.CreateDomain ("SecondaryAppDomain",
            //                                         AppDomain.CurrentDomain.Evidence, 
            //                                         appDomainSetup);
            this.appDomain = AppDomain.CreateDomain("SecondaryAppDomain", AppDomain.CurrentDomain.Evidence);

            // Create an instance of the RemoteMethods class in the secondary domain
            // and get an interface to that instance.  This ensures you can use the 
            // instance but are not loading the instance into the primary AppDomain.
            remoteInterface = (IWSSUtilities)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().GetName().FullName, typeof(WSSUtilities).FullName);

        }  // End of RemoteInterfaceFactory

        public void Dispose()
        {

            if (this.remoteInterface != null)
            {
                this.remoteInterface.Dispose();
                this.remoteInterface = null;
            }

            if (this.appDomain != null)
            {
                AppDomain.Unload(appDomain);
                this.appDomain = null;
            }

        }  // End of Dispose

    }  // End of RemoteInterfaceFactory class
}